<style type="text/css">
</style>

<template>
    <div>
        <h4>系统操作权限表</h4>
        <Alert>
            整体系统的操作权限表，当有新的Api或页面更新时，需要在此处进行编辑指定。
        </Alert>
        <ButtonGroup>
            <Button type="success" @click="addModal=true">
                <Icon type="md-add-circle"/>
                添加
            </Button>
            <Button type="warning" :disabled="deleteBtnEnable" @click="remove">
                <Icon type="md-close-circle"/>
                删除
            </Button>
        </ButtonGroup>
        <Row>
            <Col span="9">
                <Table :context="self" :data="tableData" :columns="tableColumns" stripe border
                       @on-select="tableItemChecked" @on-select-cancel="tableItemChecked"
                       @on-select-all="tableItemChecked"
                       @on-select-all-cancel="tableItemChecked"
                       style="margin-top:15px;">
                </Table>
            </Col>
        </Row>

<!--        <Modal v-model="addModal" title="添加" @on-ok="handleSubmitAdd('formValidate')"-->
<!--               @on-cancel="addModal =false">-->
<!--            <Form ref="formValidate" :model="formValidate"  :rules="ruleValidate" :label-width="100">-->
<!--                <Form-item label="区域名称" prop="Areatitle">-->
<!--                    <i-input v-model="formValidate.Areatitle" placeholder="请输入区域名称"></i-input>-->
<!--                </Form-item>-->
<!--            </Form>-->
<!--        </Modal>-->
        <Modal v-model="addModal" title="添加权限" ok-text="保存" :footer-hide="modalFooter">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
                <FormItem label="权限名称" prop="Title">
                    <Row>
                        <Col span="11">
                            <Input v-model="formValidate.Title" placeholder="请输入权限名称"/>
                        </Col>
                    </Row>
                </FormItem>
                <FormItem label="响应Api" prop="Api">
                    <Row>
                        <Col span="11">
                            <Input v-model="formValidate.Api" placeholder="请填写响应API的地址"/>
                        </Col>
                    </Row>
                </FormItem>
                <FormItem label="响应页面" prop="Page">
                    <Row>
                        <Col span="11">
                            <Input v-model="formValidate.Page" placeholder="请填写响应Page的地址"/>
                        </Col>
                    </Row>
                </FormItem>
                <FormItem>
                    <Button @click="handleSubmitAdd('formValidate')" type="primary">添加</Button>
                    <Button style="margin-left: 8px" @click="addModal = false">返回</Button>
                </FormItem>
            </Form>
        </Modal>
    </div>
</template>

<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                self: this,
                tableData: [],
                deleteBtnEnable: true,
                tableColumns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '权限名称',
                        key: 'Title',
                    },
                    {
                        title: '响应API',
                        key: 'Api',
                    },
                    {
                        title: '响应Page',
                        key: 'Page',
                    },
                ],
                selectedItems: [],
                addModal: false,
                formValidate: {
                    //添加字段
                    Title: '',
                    Api: '',
                    Page: '',
                },
                ruleValidate: {
                    Title: [
                        {required: true, message: '权限名称不能为空', trigger: 'blur'}
                    ],
                    Api: [
                        {required: true, message: '响应Api不能为空', trigger: 'blur'}
                    ],
                    Page: [
                        {required: true, message: '响应页面不能为空', trigger: 'blur'}
                    ],
                },
                modalFooter: true,
            }
        },
        mounted() {
            this.mockTableData();
        },
        methods: {
            //跳转
            jumpPage: function (id, url) {
                //跳转
                let route = '';
                if (id > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id
                        },
                    };
                } else {
                    route = {
                        path: url,
                    };
                }

                this.$router.push(route);
            },
            mockTableData() {
                let that = this;

                let para = {};
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.GetRightTable, para, function (res) {
                    that.$Loading.finish();
                    that.tableData = res.Msg;
                });

            },
            changePage(row) {
                // 这里直接更改了模拟的数据，真实使用场景应该从服务端获取数据
                console.log("row", row);
                this.current = row;
                this.mockTableData();
            },
            // 表格内选中及取消选中操作
            tableItemChecked: function (selection, row) {

                this.selectedItems = selection;
                console.log('选择删除项:', selection);

                if (this.selectedItems.length > 1) {
                    // 此处添加不要多选的提示
                    this.$Message.warning('此处不允许批量删除 ')
                    this.deleteBtnEnable = true;

                } else {
                    this.deleteBtnEnable = false;
                }
            },
            //删除
            remove: function () {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '操作警告',
                    content: '即将删除选中的权限项,该操作不可逆转，请确认您的操作。',
                    onOk: function () {
                        this.$Loading.start();
                        var selectedItemIds = "";
                        for (var i = 0; i < that.selectedItems.length; i++) {
                            selectedItemIds += that.selectedItems[i].Id.toString() + ",";
                        }
                        selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
                        let para = {id: selectedItemIds};
                        let params = that.$qs.stringify(para);

                        apiUtil.post(that, apiUtil.urls.DelRight, params, function (res) {
                            that.$Message.success('删除成功。');
                            that.mockTableData();
                        });
                    }
                });
            },
            //添加
            handleSubmitAdd(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.addData(model);
                });
            },
            addData: function (model) {
                let that = this;

                let params = that.$qs.stringify(model);
                this.$Loading.start();

                console.log(params);

                apiUtil.post(that, apiUtil.urls.AddRight, params, function (res) {
                    that.$Loading.finish();
                    that.addModal = false;
                    that.mockTableData();
                    that.$Message.info('添加成功 ')
                    that.handleReset('formValidate');

                });
            },
            //重置表单
            handleReset(name) {
                this.$refs[name].resetFields();
            }

        },
    }
</script>
